package com.qf.team2.netdisc.controller;

import com.qf.team2.netdisc.common.base.BaseController;
import com.qf.team2.netdisc.common.pojo.qo.UserQO;
import com.qf.team2.netdisc.common.pojo.vo.RespResultVO;
import com.qf.team2.netdisc.service.DocumentInsertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 创建文件夹业务接口
 *
 * @author Simon_Wong
 * @date 2021/05/26 10:35
 */
@Controller
@RequestMapping("document")
@Api(tags = "文件相关接口")
public class DocumentInsertController extends BaseController<Object> {
    @Autowired
    private DocumentInsertService documentInsertService;

    @ResponseBody
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation("文件夹创建接口")
    public RespResultVO<Object> upload(@ApiParam(value = "文件夹创建") HttpServletRequest request,String url,String name) {
        try {
            String code="";
            Integer userId=Integer.parseInt(code);
            Cookie[] cks=request.getCookies();
            //遍历Cookie
            for (Cookie ck : cks) {
                //检索出自己的Cookie
                if ("userId".equals(ck.getName())) {
                    //记录Cookie的值
                    code=ck.getValue();
                    break;
                }
            }
            Integer folderId=documentInsertService.insertNewFile(url, name, userId);
            return successResp(folderId,"创建成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return failedResp(e.getMessage());
        }

    }
}
